package com.iyimo.meta
{
	import com.mice.meta.FighterMeta;
	import com.mice.meta.MetaManager;
	
	import flash.utils.Dictionary;

	public class MetaParser{
		/**
		 * 从txt加载进来的原始数据.会在之后进行解析
		 */		
		public static var rawMeta:Dictionary = new Dictionary();
		
		public static function startParse():void{
			for (var prop:String in rawMeta){
				switch (prop){
					case "fighter":
						MetaManager.fighterMeta = parse2(rawMeta[prop], "level", "id", FighterMeta);
						break;
				}
			}
		}
		
		/**
		 * 解析key-value类meta
		 */		
		public static function parse(rawData:Array, firstKey:String, metaClass:Class):Dictionary{
			var dict:Dictionary = new Dictionary();
			
			//循环所有的原始obj
			for each (var data:Object in rawData){
				var target:Object = new metaClass();
				dict[data[firstKey]] = target;
				
				for (var key:String in data){
					if (target.hasOwnProperty(key))
						target[key] = data[key];
				}
			}
			
			return dict;
		}
		
		/**
		 * 解析key-key-value类meta
		 * rawData 从文本读取的原始数据
		 * firstKey
		 * secondKey
		 * metaClass
		 */		
		public static function parse2(rawData:Array, firstKey:String, secondKey:String, metaClass:Class):Dictionary{
			var dict:Dictionary = new Dictionary();
			
			//循环所有的原始obj
			for each (var data:Object in rawData){
				var firstValue:* = data[firstKey];
				var secondValue:* = data[secondKey];
				if (dict[firstValue] == null)
					dict[firstValue] = new Dictionary();
				
				var target:Object = new metaClass();
				dict[firstValue][secondValue] = target;
				for (var key:String in data){
					if (target.hasOwnProperty(key))
						target[key] = data[key];
				}
			}
			
			return dict;
		}
	}
}